package webaction;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.commons.lang3.StringUtils;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bean.DropListBean;

import common.DbUtil;

@Controller
@RequestMapping("/dropInfo")
public class AjaxGetDropAction {
	private Map<String, String> dropSqlMap = new HashMap<String, String>();

	private Map<String, List<DropListBean>> dropListMap = new HashMap<String, List<DropListBean>>();

	@RequestMapping(value = "/getDropValue", method = RequestMethod.POST)
	@ResponseBody
	public String getDropValue(HttpServletRequest request, HttpSession session) {
		String dropName = request.getParameter("dropName");
		String dropCode = request.getParameter("dropCode");		
		//System.err.println(dropListMap.size());
		String dropValue=getDropLabel(dropName, dropCode);
		if(!StringUtils.isBlank(dropValue))
			return dropValue;
		
		return dropCode;
	}

	@RequestMapping(value = "/getDropList", method = RequestMethod.POST)
	@ResponseBody
	public String getDropList(HttpServletRequest request, HttpSession session) throws SQLException {
		String dropName = request.getParameter("dropName");
		String dropCode = request.getParameter("dropCode");
		// String ret=(String) DbUtil.getJdbcTemplate().queryForObject("select
		// param_value from sys_param_value where
		// param_type_code='"+dropName+"'"
		// , String.class);
		Object[] obj = new Object[1];
		obj[0] = dropName;
		List<String> list = DbUtil.getJdbcTemplate().queryForList("select param_value from sys_param_value where param_type_code=?", obj, String.class);
		if (list.size() > 0)
			return list.get(0);
		return "";
	}

	/**
	 * 存储drop信息
	 * 
	 * @param dropName
	 * @param dropCode
	 */
	public void cachedDropData(String dropName, String dropCode) {
		if (dropCode == null||dropName==null) {
			return;
		}
		List<Map<String, Object>> list = null;
		String ext_sql = null;
		Object[] obj = new Object[1];
		obj[0] = dropName;
		try {
			if (!dropListMap.containsKey(dropName)) {
				if (dropSqlMap.containsKey(dropName))
					ext_sql = dropSqlMap.get(dropName);
				else {
					ext_sql = DbUtil.getJdbcTemplate().queryForObject("select ext_sql from sys_extend_property where ext_code=?", obj, String.class);
					dropSqlMap.put(dropName, ext_sql);
				}
			}
		} catch (EmptyResultDataAccessException e) {
			e.printStackTrace();
			ext_sql = null;
		}
		try {
			if (!StringUtils.isBlank(ext_sql)) {
				list = DbUtil.getJdbcTemplate().queryForList(ext_sql);
				if (!dropListMap.containsKey(dropName)) {
					List<DropListBean> dropList = new ArrayList<DropListBean>();
					String value = null;
					String label = null;
					String filter = null;
					for (Map<String, Object> m : list) {
						value = m.get("VALUE") == null ? "" : m.get("VALUE").toString();
						label = m.get("LABEL") == null ? "" : m.get("LABEL").toString();
						filter = m.get("FILTER") == null ? "" : m.get("FILTER").toString();
						dropList.add(new DropListBean(value, label, filter));

					}
					dropListMap.put(dropName, dropList);
				}
			}
		} catch (EmptyResultDataAccessException e) {
			e.printStackTrace();
		}

	}
/**
 * 获得指定code对应的value值
 * @param dropName
 * @param dropCode
 * @return
 */
	public String getDropLabel(String dropName, String dropCode) {
		if (dropCode == null||dropName==null) {
			return null;
		}
		if (!dropListMap.containsKey(dropName)) {
			cachedDropData(dropName, dropCode);
		}
		List<DropListBean> list=dropListMap.get(dropName);
		String label=dropCode;
		for(DropListBean bean:list){
			if(dropCode.equals(bean.getValue()))
				label=bean.getLabel();
		}
		return label;
	}

}
